"""
@Project:xiaomishop
@File:auth.py
@Author:马可
@Date:下午 02:20
"""

# 使用手机号登陆

from django.contrib.auth.backends import ModelBackend
from django.contrib.auth.hashers import check_password

from users.models import User
from django.db.models import Q
class AuthBackend(ModelBackend):
    def authenticate(self, request, username=None, password=None, **kwargs):
        """从写字方法"""
        # 1.用户输入的可能是用户名或手机号
        # 使用q对象进行逻辑操作
        try:
            user = User.objects.get(Q(username=username) | Q(mobile=username))
        except Exception as e:
            print(e)
            return None
        # 密码校验
        is_correct = check_password(password,user.password)
        if is_correct:
            return user
        else:
            print("密码错误")
            return None